<?php
namespace App\Repos;

use App\Models\Favorite;

class FavoriteRepo
{
    /**
     * @param $openid
     * @param $productId
     * @return Favorite Model |null
     */
    public static function isFavorite($openid,$productId)
    {

        $param['openid']      = $openid;
        $param['is_favorite'] = 1;
        $param['product_id']  = $productId;

        return Favorite::where($param)->select('openid','is_favorite')->get();
    }

    /** 加入收藏夹
     * @param $request
     * @return mixed
     */
    public static function  changeFavoriteState($request)
    {
        $param['openid']     = trim($request->openid);
        $param['product_id'] = trim($request->productId);

        $rs['data']   = '';
        $rs['status'] = false;

        try {
            $changeStatus = Favorite::updateOrCreate($param,['is_Favorite'=>(int)$request->isFavorite]);
            if ($changeStatus) {
                $rs['data']   = (int)$request->isFavorite == 0 ? 0 : 1;
                $rs['status'] = true;
            }
        } catch (\Exception $e) {
            return $rs;
        }

        return $rs;

    }

    /**
     * @param $openid
     * @return mixed
     */
    public static function getFavoriteStateList($openid)
    {
        return Favorite::with('product')->where(['openid'=>$openid])->get();
    }

    /**
     * @param $openid
     * @return mixed
     */
    public static function getFavoriteNum($openid)
    {
        return Favorite::where(['openid'=>$openid])->count();
    }


}